package cn.spream.common.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * web助手
 * Created with IntelliJ IDEA.
 * User: shaojiaxin
 * Date: 13-7-13
 * Time: 下午4:37
 * To change this template use File | Settings | File Templates.
 */
public class WebHelper {

    /**
     * 根据name获得cookie中的value
     *
     * @param httpServletRequest
     * @param name
     * @return
     */
    public static String getCookieValue(HttpServletRequest httpServletRequest, String name) {
        String value = null;
        Cookie cookie = getCookie(httpServletRequest, name);
        if (cookie != null) {
            value = cookie.getValue();
        }
        return value;
    }

    /**
     * 根据name获得cookie对象
     *
     * @param httpServletRequest
     * @param name
     * @return
     */
    public static Cookie getCookie(HttpServletRequest httpServletRequest, String name) {
        Cookie result = null;
        Cookie cookies[] = httpServletRequest.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    result = cookie;
                    break;
                }
            }
        }
        return result;
    }

    /**
     * 设置cookie
     *
     * @param httpServletResponse
     * @param cookie
     */
    public static void setCookie(HttpServletResponse httpServletResponse, Cookie cookie) {
        httpServletResponse.addCookie(cookie);
    }

    /**
     * 删除cookie
     *
     * @param httpServletResponse
     * @param name
     */
    public static void removeCookie(HttpServletResponse httpServletResponse, String name) {
        Cookie cookie = new Cookie(name, null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        httpServletResponse.addCookie(cookie);
    }

    /**
     * 获取请求的ip,多个ip使用逗号隔开
     *
     * @param httpServletRequest
     * @return
     */
    public static String getIpAddress(HttpServletRequest httpServletRequest) {
        String ip = httpServletRequest.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = httpServletRequest.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = httpServletRequest.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = httpServletRequest.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取request的请求全路径，包括GET请求参数
     *
     * @param httpServletRequest
     * @return
     */
    public static String getRequestURLAndQueryString(HttpServletRequest httpServletRequest) {
        String url = httpServletRequest.getRequestURL().toString();
        if (url.endsWith("/")) {
            url = url.substring(0, url.length() - 1);
        }
        String queryString = httpServletRequest.getQueryString();
        if (queryString != null && queryString.length() > 0) {
            url += "?" + queryString;
        }
        return url;
    }

}
